//
//  ICNoticeDrtNewsCell.m
//  ICome
//
//  Created by zhangrongwu on 2017/7/21.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICNoticeDrtNewsCell.h"
#import "ICHeaderFile.h"
@interface ICNoticeDrtNewsCell()

@property (nonatomic, strong)UIImageView *imageV;

@property (nonatomic, strong)UILabel *subject;

@property (nonatomic, strong)UILabel *summary;

@end
@implementation ICNoticeDrtNewsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentV addSubview:self.subject];
        [self.contentV addSubview:self.imageV];
        [self.contentV addSubview:self.summary];
        
        [self setUI];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.contentView addGestureRecognizer:tap];
    }
    return self;
}

- (void)tapAction {
    [self routerEventWithName:GXRouterEventDrtNewsTapEvent
                     userInfo:@{TypeDrtNews : self.modelFrame}];
}


- (void)setUI {
    
    [self.subject mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(12);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.top.equalTo(self.lineView.mas_bottom).offset(10);
    }];
    
    [self.imageV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(12);
        make.top.equalTo(self.subject.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(90, 60));
    }];
    
    [self.summary mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageV.mas_right).offset(10);
        make.right.equalTo(self.bgView.mas_right).offset(-10);
        make.top.equalTo(self.imageV.mas_top);
    }];
}


-(void)setModelFrame:(ICMessageFrame *)modelFrame {
    [super setModelFrame:modelFrame];
    
    self.subject.text = modelFrame.model.message.content;
    
    NSDictionary *filekey =  [NSDictionary dictionaryWithJsonString:modelFrame.model.message.fileKey];
    if (![ICTools stringEmpty:filekey[@"filekey"]]) {
        [self.imageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(90, 60));
            make.left.equalTo(self.bgView.mas_left).offset(12);
        }];
    } else {
        [self.imageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(0, 0));
            make.left.equalTo(self.bgView.mas_left).offset(2);
        }];
    }
    
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:[ICTools getFileMaxImageURL:(filekey[@"filekey"])]] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor]]];
    
    if(![ICTools stringEmpty:filekey[@"summary"]]){
        self.summary.hidden = NO;
        self.summary.text = filekey[@"summary"]; //[message.dictkey objectForKey:@"subject"];
        [self.summary sizeToFit];
        // 强制更新
        [self layoutIfNeeded];
        [self.summary mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.summary.height);
        }];
    } else {
        self.summary.hidden = YES;
        [self.summary mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
}

// JISUAN
+(CGFloat)calculateheight:(NSString *)str
{
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width - 60, 15)];
    label.font = [UIFont systemFontOfSize:14];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentLeft;
    label.text = str;
    [label sizeToFit];
    return label.frame.size.height;
}

- (UIImageView *)imageV
{
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        _imageV.layer.masksToBounds = YES;
        _imageV.layer.cornerRadius = 5;
        _imageV.clipsToBounds = YES;
        _imageV.userInteractionEnabled = YES;
        
    }
    return _imageV;
}

-(UILabel *)subject {
    if (!_subject) {
        _subject = [[UILabel alloc] init];
        _subject.numberOfLines = 2;
        _subject.textColor = ICRGB(0x333333);
        _subject.font = [UIFont systemFontOfSize:16];
    }
    return _subject;
}

-(UILabel *)summary {
    if (!_summary) {
        _summary = [[UILabel alloc] init];
        _summary.numberOfLines = 3;
        _summary.textColor = ICRGB(0x999999);
        _summary.font = [UIFont systemFontOfSize:14];
    }
    return _summary;
}

@end
